<template>
  <div class="waiting">
    <span class="tips">登录中...</span>
  </div>
</template>

<script>
import { loginFromPlatform } from '@/api/user'
import cacheUtil from '@/utils/cache'
import { getQueryParams } from '@/utils/params'

export default {
  name: 'LoginFromPlatform',
  mounted() {
    const queryParams = getQueryParams(location.href)
    // console.log(queryParams)
    if (queryParams.code) {
      const code = queryParams.code
      // console.log('redirect url code =', code)
      this.doLogin(code)
    }
  },
  methods: {
    doLogin(code) {
      loginFromPlatform(code).then((res) => {
        if (res.success) {
          if (!res.data.moduleTree) {
            this.$message.error(
              '该用户无菜单模块权限，无法登录！'
            )
            return
          }
          cacheUtil.setItem(
            'menu',
            JSON.stringify(res.data.moduleTree)
          )
          cacheUtil.setItem('token', res.data.token)
          cacheUtil.setItem('userName', res.data.userName)
          cacheUtil.setItem('deptName', res.data.deptName)
          cacheUtil.setItem('status', res.data.userLevel)
          location.replace('/future')
          // this.$router.replace('/')
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.waiting {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .tips {
    color: #333;
    font-size: 30px;
  }
}
</style>
